home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / DCPLIB.C < prev    next >
C/C++ Source or Header  |  1991-12-07  |  13KB  |  326 lines

  1. /*
  2.    For best results in visual layout while viewing this file, set
  3.    tab stops to every 4 columns.
  4. */
  5.  
  6. /*
  7.    d c p l i b c
  8.  
  9.    DCP system-dependent library
  10.  
  11.    Services provided by dcplib.c:
  12.  
  13.    - login
  14.    - UNIX commands simulation
  15.  
  16.    Updated:
  17.  
  18.       14May89  - Added system name to login prompt - ahd
  19.                  Added configuration file controlled user id, password
  20.                  Added Kermit server option
  21.       17May89  - Redo login processing to time out after five minutes;
  22.                  after all, we have to exit someday.                    ahd
  23.       22Sep89  - Add password file processing                           ahd
  24.       24Sep89  - Modify login() to issue only one wait command for up
  25.                  to 32K seconds; this cuts down LOGFILE clutter.        ahd
  26.       01Oct89  - Re-do function headers to allow copying for function
  27.                  prototypes in ulib.h                                   ahd
  28.       17Jan90  - Filter unprintable characters from logged userid and
  29.                  password to prevent premature end of file.             ahd
  30.       18Jan90  - Alter processing of alternate shells to directly
  31.                  invoke program instead of using system() call.         ahd
  32.    6  Sep 90   - Change logging of line data to printable               ahd
  33.       8 Sep 90 - Split ulib.c into dcplib.c and ulib.c                  ahd
  34.       8 Oct 90 - Break rmail.com and rnews.com out of uuio
  35.                  Add FIXED_SPEED option for no-autobauding              ahd
  36.       10Nov 90 - Move sleep call into ssleep and rename                 ahd
  37. */
  38.  
  39. #include <ctype.h>
  40. #include <direct.h>
  41. #include <dos.h>
  42. #include <process.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <sys/types.h>
  47. #include <time.h>
  48.  
  49. #ifdef __TURBOC__
  50. #include <sys/timeb.h>
  51. #endif
  52.  
  53. #include "lib.h"
  54. #include "arpadate.h"
  55. #include "dcp.h"
  56. #include "dcplib.h"
  57. #include "dcpsys.h"
  58. #include "hlib.h"
  59. #include "hostable.h"
  60. #include "import.h"
  61. #include "modem.h"
  62. #include "pushpop.h"
  63. #include "security.h"
  64. #include "ssleep.h"
  65. #include "ulib.h"
  66. #include "usertabl.h"
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*        Define current file name for panic() and printerr()         */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. currentfile();
  73.  
  74. /*--------------------------------------------------------------------*/
  75. /*                    Internal function prototypes                    */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. static void LoginShell( const   struct UserTable *userp );
  79.  
  80. /*--------------------------------------------------------------------*/
  81. /*    l o g i n                                                       */
  82. /*                                                                    */
  83. /*    Login handler                                                   */
  84. /*--------------------------------------------------------------------*/
  85.  
  86. CONN_STATE login(void)
  87. {
  88.    char line[BUFSIZ];                  /* Allow for long domain names!  */
  89.    char user[50];
  90.    char pswd[50];
  91.    char attempts = 0;                  /* Allows login tries         */
  92.    char *token;                        /* Pointer to returned token  */
  93.    struct UserTable *userp;
  94.  
  95.  
  96. /*--------------------------------------------------------------------*/
  97. /*    Our modem is now connected.  Begin actual login processing      */
  98. /*    by displaying a banner.                                         */
  99. /*--------------------------------------------------------------------*/
  100.  
  101.    ssleep(1);
  102.    sprintf(line,"\r\n\n%s %d.%02d (%s) (%s)\r\n",
  103. #ifdef __TURBOC__
  104.             "MS-DOS",
  105. #else
  106.             (_osmode == DOS_MODE) ? "MS-DOS" : "OS/2" ,
  107. #endif
  108.             _osmajor, _osminor,
  109.        domain, device);   /* Print a hello message            */
  110.    wmsg(line,0);
  111.    ddelay(250);
  112.  
  113. /*--------------------------------------------------------------------*/
  114. /*    Display a login prompt until we get a printable character or    */
  115. /*    the login times out                                             */
  116. /*--------------------------------------------------------------------*/
  117.  
  118.    for ( attempts = 0; attempts < 5 ; attempts++ )
  119.    {
  120.       boolean invalid = TRUE;
  121.       while (invalid)         /* Spin for a user id or timeout       */
  122.       {
  123.          wmsg("\r\nlogin: ", 0);
  124.          strcpy(user,"");
  125.          if (rmsg(user, 2, 30) == TIMEOUT) /* Did the user enter data?  */
  126.             return CONN_DROPLINE;   /* No --> Give up                */
  127.  
  128.          if (equal(user,"NO CARRIER"))
  129.             return CONN_DROPLINE;
  130.  
  131.          token = user;
  132.          while ((*token != '\0') && invalid) /* Ingore empty lines   */
  133.             invalid = ! isgraph(*token++);
  134.       } /* while */
  135.  
  136. /*--------------------------------------------------------------------*/
  137. /*          Zap unprintable characters, then log the user id          */
  138. /*--------------------------------------------------------------------*/
  139.  
  140.       while (*token != '\0')
  141.       {
  142.          if (*token < ' ')
  143.             *token = '?';
  144.          token++;
  145.       }
  146.  
  147.       printmsg(14, "login: login=%s", user);
  148.  
  149. /*--------------------------------------------------------------------*/
  150. /*               We have a user id, now get a password                */
  151. /*--------------------------------------------------------------------*/
  152.  
  153.       wmsg("\r\nPassword: ", 0);
  154.       strcpy(pswd,"");
  155.       if (rmsg(pswd, 0, 30) == TIMEOUT)
  156.          return CONN_DROPLINE;
  157.  
  158. /*--------------------------------------------------------------------*/
  159. /*       Zap unprintable characters before we log the password        */
  160. /*--------------------------------------------------------------------*/
  161.  
  162.       token = pswd;
  163.       while (*token != '\0')
  164.       {
  165.          if (*token < ' ')
  166.             *token = '?';
  167.          token++;
  168.       }
  169.  
  170.       printmsg(14, "login: password=%s", pswd);
  171.  
  172. /*--------------------------------------------------------------------*/
  173. /*                 Validate the user id and passowrd                  */
  174. /*--------------------------------------------------------------------*/
  175.  
  176.       userp = checkuser(user);         /* Locate user id in host table  */
  177.  
  178.       if (userp == BADUSER)            /* Does user id exist?           */
  179.       {                                /* No --> Notify the user        */
  180.          wmsg("\r\nlogin failed",0);
  181.  
  182.          token = user;
  183.          while (!isalnum( *token ) && (*token !=  '\0'))
  184.             token ++;                  /* Scan for first alpha-numeric  */
  185.  
  186.          if (*token != '\0')           /* If at least one good char     */
  187.             printmsg(0,"login: login for user %s failed, bad user id",
  188.                   user);               /* Log the error for ourselves   */
  189.       }
  190.       else if ( equal(pswd,userp->password))   /* Correct password?     */
  191.       {                                /* Yes --> Log the user "in"     */
  192.                    /*   . . ..+....1....  +....2....+....3....  + .   */
  193.          sprintf(line,"\r\n\nWelcome to %s; login complete at %s\r\n",
  194.                   domain, arpadate());
  195.          wmsg(line, 0);
  196.          printmsg(0,"login: login user %s (%s) at %s",
  197.                      userp->uid, userp->realname, arpadate());
  198.  
  199.          if equal(userp->sh,UUCPSHELL) /* Standard uucp shell?       */
  200.          {
  201.             securep = userp->hsecure;
  202.             return CONN_PROTOCOL;   /* Yes --> Startup the machine   */
  203.          }
  204.          else {                     /* No --> run special shell      */
  205.             LoginShell( userp );
  206.             return CONN_DROPLINE;   /* Hang up phone and exit        */
  207.          }
  208.       }
  209.       else {                        /* Password was wrong.  Report   */
  210.          wmsg("\r\nlogin failed",0);
  211.          printmsg(0,"login: login user %s (%s) failed, bad password %s",
  212.                   userp->uid, userp->realname, pswd);
  213.       }
  214.    }  /* for */
  215.  
  216. /*-----------------------------------------------------------------*/
  217. /*    If we fall through the loop, we have an excessive number of  */
  218. /*    login attempts; hangup the telephone and try again.          */
  219. /*-----------------------------------------------------------------*/
  220.  
  221.    return CONN_DROPLINE;      /* Exit the program                    */
  222.  
  223. } /*login*/
  224.  
  225. /*--------------------------------------------------------------------*/
  226. /*    l o g i n b y p a s s                                           */
  227. /*                                                                    */
  228. /*    Initialize user setup when login is bypassed                    */
  229. /*--------------------------------------------------------------------*/
  230.  
  231. CONN_STATE loginbypass(const char *user)
  232. {
  233.    struct UserTable *userp;
  234.    char line[BUFSIZ];                  /* Allow for long domain names!  */
  235.  
  236.    printmsg(14, "loginbypass: login=%s", user);
  237.  
  238. /*--------------------------------------------------------------------*/
  239. /*                 Validate the user id                               */
  240. /*--------------------------------------------------------------------*/
  241.  
  242.    userp = checkuser(user);         /* Locate user id in host table  */
  243.  
  244.    if (userp == BADUSER)            /* Does user id exist?           */
  245.    {                                /* No --> Notify the user        */
  246.       wmsg("\r\nUUCICO login failed",0);
  247.  
  248.       printmsg(0,"loginbypass: login for user %s failed, bad user id",
  249.                user);               /* Log the error for ourselves   */
  250.       return CONN_DROPLINE;         /* Hang up phone and exit        */
  251.    }
  252.    else {
  253.                                     /* Yes --> Log the user "in"     */
  254.                 /*   . . ..+....1....  +....2....+....3....  + .   */
  255.       sprintf(line,"\r\n\nWelcome to %s; login complete at %s\r\n",
  256.                domain, arpadate());
  257.       wmsg(line, 0);
  258.       printmsg(0,"loginbypass: login user %s (%s) at %s",
  259.                   userp->uid, userp->realname, arpadate());
  260.  
  261.       if equal(userp->sh,UUCPSHELL) /* Standard uucp shell?       */
  262.       {
  263.          securep = userp->hsecure;
  264.          return CONN_PROTOCOL;   /* Yes --> Startup the machine   */
  265.       } /* if equal(userp->sh,UUCPSHELL) */
  266.       else {                     /* No --> run special shell      */
  267.          LoginShell( userp );
  268.          return CONN_DROPLINE;   /* Hang up phone and exit        */
  269.       } /* else */
  270.    } /* else */
  271.  
  272. } /*loginbypass*/
  273.  
  274. /*--------------------------------------------------------------------*/
  275. /*    L o g i n S h e l l                                             */
  276. /*                                                                    */
  277. /*    Execute a non-default remote user shell                         */
  278. /*--------------------------------------------------------------------*/
  279.  
  280. static void LoginShell( const   struct UserTable *userp )
  281. {
  282.    char *shellstring;
  283.    char *path;
  284.    char *args;
  285.    int   rc;
  286.  
  287. /*--------------------------------------------------------------------*/
  288. /*              Get the program to run and its arguments              */
  289. /*--------------------------------------------------------------------*/
  290.  
  291.    shellstring = strdup(userp->sh);
  292.                            /* Copy user shell for parsing   */
  293.    path = strtok(shellstring," \t");   /* Get program name  */
  294.    args = strtok(NULL,"");    /* Get rest of arg string     */
  295.  
  296.    printmsg(1,"LoginShell: Invoking %s in directory %s",
  297.          userp->sh, userp->homedir);
  298.  
  299.    ddelay(250);            /* Wait for port to stablize     */
  300.  
  301. /*--------------------------------------------------------------------*/
  302. /*       Run the requested program in the user's home directory       */
  303. /*--------------------------------------------------------------------*/
  304.  
  305.    PushDir(userp->homedir);/* Switch to user's home dir     */
  306.    if (args == NULL)
  307.       rc = spawnl(P_WAIT, path, path, NULL);
  308.    else
  309.       rc = spawnl(P_WAIT, path, path, args, NULL);
  310.  
  311.    PopDir();               /* Return to original directory  */
  312.  
  313. /*--------------------------------------------------------------------*/
  314. /*                     Report any errors we found                     */
  315. /*--------------------------------------------------------------------*/
  316.  
  317.    if ( rc < 0 )           /* Error condition?              */
  318.    {                        /* Yes --> Report it to the user */
  319.       printmsg(0,"LoginShell: Unable to execute user shell");
  320.       printerr(path);
  321.    }
  322.    else                    /* No --> Report normal result   */
  323.       printmsg(rc == 0 ? 4 : 0,"LoginShell: %s return code is %d", path, rc);
  324.  
  325. } /* LoginShell */
  326.